var member;
//存放所有的地址信息
var regionList;
var cityList;
var countyList;
var addressMaxCount=10;
var addressNowCount=0;
$(function(){
	//获取cookie中的对象
	member=getEntity("memberInfo");
	if(member){
		$("#mName").html("会员名："+member.mName);
		$("#mPhone").html("绑定手机："+member.mPhone+"<a href=''>修改手机</a>");
		$("#mImg").attr("src",member.mImg);
		$("#mId").val(member.mId);
	}else{
		window.location.href=rootPath+"/";
	}
	//添加常用地址
	$("#confirmAddAddress").click(function(){
		var data = $("form").serialize();
		if(!$("#maId")){
			if(addressMaxCount-addressNowCount<=0){
				return false;
			}
		}
		$.ajax({
			type:"post",
			url:rootPath + "/address/addOrUpdateAddress",
			dataType:"JSON",
			data:data,
			success:function(data){
				if(data.resultSuccess){
					location.reload(true);
				}
			}
		});
	});
	//展示用户的常用地址
	$.ajax({
		type:"get",
		url:rootPath + "/address/selectBymId?mId="+member.mId,
		dataType:"JSON",
		async: false,
		success:function(data){
			var addressList=data.data;
			addressNowCount=addressList.length;
			$("#red").text("");
			$("#red").text("您已创建了"+addressNowCount+"个收货地址，还能创建"+(addressMaxCount-addressNowCount)+"条地址");
			$.each(addressList,function(i,item){
				var address="";
				var postcode="";
				if(item.postcode){
					postcode=item.postcode;
				}else{
					postcode="无";
				}
				address="<li class='item'>"
					+"<p>"+item.name+"</p>"
					+"<p class='area'>"+item.province+item.city+item.county+"</p>"
					+"<p class='area twoline'>"+item.streetAll+"</p>"
					+"<p>"+postcode+"</p>"
					+"<p>"+item.tel+"</p>"
					+"<p class='operate'><a href='javascript:selectAddressById(\""+item.maId+"\");'>修改|</a><a href='javascript:deleteAddressById(\""+item.maId+"\");' class='delete'>删除</a></p>";
				$("#addressList").append(address);
			})
		}
	})
	//获取并分级展示全国的所有地址信息
	$.ajax({
		type:"get",
		url:rootPath + "/address/selectAllRegion",
		dataType:"JSON",
		async: false,
		success:function(data){
			regionList=data.data;
			$.each(regionList,function(i,item){
				$("#provinceCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			var regionCode=$("#provinceCode").val();
			cityList=regionList[0].city;
			$("#countyCode").empty();
			$.each(cityList,function(i,item){
				$("#cityCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			countyList=cityList[0].area;
			$("#countyCode").empty();
			$.each(countyList,function(i,item){
				$("#countyCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			$("#province ").val($("#provinceCode").find("option:selected").text());
			$("#city ").val($("#cityCode").find("option:selected").text());
			$("#county").val($("#countyCode").find("option:selected").text());
		}
	})
	//实现省市县的三级联动
	$("#provinceCode").change(function(){
		var regionCode=$("#provinceCode").val();
		$.each(regionList,function(i,item){
			if(regionCode==item.regionCode){
				cityList=item.city;
			}
		})
		$("#cityCode").empty();
		$("#countyCode").empty();
		$.each(cityList,function(i,item){
			$("#cityCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
		})
		countyList=cityList[0].area;
		$.each(countyList,function(i,item){
			$("#countyCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
		})
		$("#province ").val($("#provinceCode").find("option:selected").text());
		$("#city ").val($("#cityCode").find("option:selected").text());
		$("#county").val($("#countyCode").find("option:selected").text());
	})
	
	$("#cityCode").change(function(){
		var regionCode=$("#cityCode").val();
		$.each(cityList,function(i,item){
			if(regionCode==item.regionCode){
				countyList=item.area;
			}
		})
		$("#countyCode").empty();
		$.each(countyList,function(i,item){
			$("#countyCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
		})
		$("#province ").val($("#provinceCode").find("option:selected").text());
		$("#city ").val($("#cityCode").find("option:selected").text());
		$("#county").val($("#countyCode").find("option:selected").text());
	});
	$("#countyCode").change(function(){
		$("#county").val($("#countyCode").find("option:selected").text());
	});
});
//修改常用地址
function selectAddressById(maId){
	$.ajax({
		type:'post',
		url:rootPath + "/address/selectAddressById?maId="+maId,
		dataType:"JSON",
		success:function(data){
			var address=data.data;
			$("#postcode").val(address.postcode);
			$("#name").val(address.name);
			$("#street").val(address.street);
			$("#maId").val(address.maId);
			$("#tel").val(address.tel);
			$("#provinceCode option[value="+address.provinceCode+"]").attr("selected",true);
			var regionCode=$("#provinceCode").val();
			$.each(regionList,function(i,item){
				if(regionCode==item.regionCode){
					cityList=item.city;
				}
			})
			$("#cityCode").empty();
			$("#countyCode").empty();
			$.each(cityList,function(i,item){
				$("#cityCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			$("#cityCode option[value="+address.cityCode+"]").attr("selected",true);
			var regionCode=$("#cityCode").val();
			$.each(cityList,function(i,item){
				if(regionCode==item.regionCode){
					countyList=item.area;
				}
			})
			$("#countyCode").empty();
			$.each(countyList,function(i,item){
				$("#countyCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			$("#countyCode option[value="+address.countyCode+"]").attr("selected",true);
			$("#province ").val(address.province);
			$("#city ").val(address.city);
			$("#county").val(address.county);
		}
	});
}
//删除常用地址
function deleteAddressById(maId){
	$.ajax({
		type:'post',
		url:rootPath + "/address/deleteAddressById?maId="+maId,
		dataType:"JSON",
		success:function(data){
			if(data.resultSuccess){
				location.reload(true);
			}
		}
	})
}
